package org.csu.mypetstore.api.controller.front;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.config.AlipayConfig;
import org.csu.mypetstore.api.entity.Order;
import org.csu.mypetstore.api.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("/payment/")
public class PaymentController {
    @Autowired
    private RedisService redisService;

    @PostMapping("alipay")
    @ResponseBody
    public CommonResponse<String> alipay(@RequestBody Order order) throws AlipayApiException {
        //获得初始化的AlipayClient
        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000117632552",
                "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCdLHla+XR/a4iZTrMD3NYDkxzie019FplwSR4kXJBmRFVLjhp6KOpyS34k8G7TmF0AEgdvIC2rU6ae9cG8aSput7IFyzJNgGPp+KBr6CkylOu1c7GGNoefBIUw+cibM2n4N3b8XuRJCGVmVQN5d/pdzdd3ownwRY/trQOVi1ySvtjg2fVntupiTmuJLH54CThE2HNo2Hk4uKr3W/o+If8GoDdlAh/Me73qSZhHf3eR9LifDbXdtRmmq0a6aQ/2Ao6tgsT401SlU+AlkB1H+OPZlfb/TiAx7TBNLAw8IYPqYDuXEvEMtzFiG0sap8PlsHmAjQ0N95MD8zZ++RtfW4kpAgMBAAECggEBAIB+2pv4szh05pFHmO0Af2seu4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PYnGpF72GVIc2CJlrHO8Vr9tk9OfM3KMWObi+KnEIEE1QgWXxdi4ICpp6VINdPqbhyYCMBmMYdUxKUO24K2M8ryE6Cni374v6R0EvCeEEfJvpzgZJemIZ2miD107k=",

                "json","UTF-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArXAOOa7GHr4xvy3tnNkc1SbAFUNkyElI8jQ9t/D3HY8jaw+kga0NKjBNXcXwwdq7ZFgWYgRIU7tHSFDtgL+/tkBqcuS0vqXcNH9pYaw0a+8nbyOytHFsbI7XmaXZlo6xMz4hKKhlJodN5LF1BhycXIBhCpNb3e3gviDyVpibF6Kmax+8lYsSPDzi5Qovcqz8FpaRn9SgWKtsDtaAd1R/R/+njrpMyaLoBSazK5GgOctMQczCU7px7T5wyLhVZ5vwcCb5OjBr55r2cLusUwps2H1UBVEXiB8C9XAGzhAq+a+TWjIm4uiVq1Elq/CA1My43snCpVkeneTVYzSaDioIDQIDAQAB",
                "RSA2");
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        String s=String.valueOf(1123);
        System.out.println(order.getOrderId()+"***********************************");
        //商户订单号，商户网站订单系统中唯一订单号，必填
        int out_trade_no =order.getOrderId();
        //付款金额，必填
        BigDecimal total_amount =order.getTotalPrice();
        //订单名称，必填
        String subject = order.getTotalPrice()+"'s Order";

        //商品描述，可空
        String body = "";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                +"\"body\":\""+body+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //输出
        System.out.println(result);

        return CommonResponse.createForSuccess(result);
    }
}
